from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import time
from weiyueyun_selenium import biz_mysql


def weiyueyun_selenium(Novel_name, insert_time, Chapter, Extension_name, Platform):
    driver = webdriver.Chrome()
    driver.get("http://new.emeixs.com/index.php/Admin/Main/index.html")
    driver.maximize_window()
    driver.implicitly_wait(20)

    # 定位账号
    driver.find_element_by_name('username').clear()
    driver.find_element_by_name('username').send_keys(username)

    # 定位密码
    driver.find_element_by_name('password').clear()
    driver.find_element_by_name('password').send_keys(password)

    # 点击登录
    yzm = input(">>请输入验证码：")
    driver.find_element_by_xpath('//*[@id="mcode"]').send_keys(yzm)
    driver.find_element_by_xpath('//a[@class="button button-block button-rounded button-royal button-large"]').click()

    # 推广中心
    try:
        WebDriverWait(driver, 60).until(
            EC.element_to_be_clickable((By.XPATH, '//*[@id="mCSB_1_container"]//li[3]/a'))
        )
        driver.find_element_by_xpath('//*[@id="mCSB_1_container"]//li[3]/a').click()
    except Exception:
        print("推广中心")

    # 搜索kuang
    try:

        time.sleep(1)
        driver.find_element_by_xpath('//*[@id="keyword"]').clear()
        driver.find_element_by_xpath('//*[@id="keyword"]').send_keys(Novel_name)
    except Exception:
        print("搜索kuang")

    # 搜素

    try:
        WebDriverWait(driver, 60).until(
            EC.element_to_be_clickable((By.XPATH, '//*[@id="btn_search"]'))
        )
        driver.find_element_by_xpath('//*[@id="btn_search"]').click()
    except Exception:
        print("搜素")

    # 文案推广

    try:
        WebDriverWait(driver, 60).until(
            EC.element_to_be_clickable((By.XPATH, '//td[6]/div[2]/a'))
        )
        driver.find_element_by_xpath('//td[6]/div[2]/a').click()
    except Exception:
        print("文案推广")

    # 获得打开的第一个窗口句柄
    window_1 = driver.current_window_handle
    # print(window_1)
    # 获得打开的所有的窗口句柄
    windows = driver.window_handles
    # print(windows)
    # 切换到最新的窗口
    for current_window in windows:
        if current_window != window_1:
            driver.switch_to.window(current_window)

    # 推广链接

    try:
        WebDriverWait(driver, 60).until(
            EC.element_to_be_clickable((By.XPATH, '//tr[1]/td[2]/a'))
        )
        driver.find_element_by_xpath('//tr[1]/td[2]/a').click()
    except Exception:
        print("推广链接")

    # 生成推广链接

    try:
        WebDriverWait(driver, 60).until(
            EC.element_to_be_clickable((By.XPATH, '//*[@id="r_in_link"]/button'))
        )
        driver.find_element_by_xpath('//*[@id="r_in_link"]/button').click()
    except Exception:
        print("生成推广链接")

    # 渠道名称

    try:
        WebDriverWait(driver, 60).until(
            EC.element_to_be_clickable((By.XPATH, '//*[@id="pen_name"]'))
        )
        driver.find_element_by_xpath('//*[@id="pen_name"]').clear()
        driver.find_element_by_xpath('//*[@id="pen_name"]').send_keys(str(insert_time) + Extension_name)
    except Exception:
        print("渠道名称")

    # 确定

    try:
        WebDriverWait(driver, 60).until(
            EC.element_to_be_clickable((By.XPATH, '//*[@id="btn_new_send"]'))
        )
        driver.find_element_by_xpath('//*[@id="btn_new_send"]').click()
    except Exception:
        print("确定失败")

    # 跳转到图片文案

    try:
        WebDriverWait(driver, 60).until(
            EC.element_to_be_clickable((By.XPATH, '//*[@id="btn-img-url"]'))
        )
        driver.find_element_by_xpath('//*[@id="btn-img-url"]').click()
    except Exception:
        print("跳转到图片文案")

    # 复制文章内容

    try:
        WebDriverWait(driver, 60).until(
            EC.element_to_be_clickable((By.XPATH, '//*[@id="btn-copy-content"]'))
        )
        driver.find_element_by_xpath('//*[@id="btn-copy-content"]').click()
    except Exception:
        print("复制文章内容")

    # 复制原文链接

    try:
        WebDriverWait(driver, 60).until(
            EC.element_to_be_clickable((By.XPATH, '//*[@id="btn-copy-url"]'))
        )
        driver.find_element_by_xpath('//*[@id="btn-copy-url"]').click()
    except Exception:
        print("复制原文链接")

    # 获取url

    try:
        time.sleep(1)
        url = driver.execute_script("return jQuery('[id=txt-referral-url]').val();")
        print(url)
        # 数据入库
        try:
            biz_mysql.insert_novel_mysql(url=url, Public_platform=Platform, Novel_name=Novel_name, Chapter=Chapter,
                                         datetime=insert_time, Extension_name=str(insert_time) + Extension_name)
        except Exception:
            print("入库失败")
    except Exception:
        print("获取url")

    driver.close()
    driver.switch_to.window(window_1)
    driver.quit()
